﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTest.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Data;
using System.Data.SqlClient;
using Dapper;
using MySql.Data.MySqlClient;
using System.Xml;
using static AspNetCoreTest.Controllers.WeatherForecastController.Test;
using System.IO;

namespace AspNetCoreTest.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class WeatherForecastController : ControllerBase, IDisposable
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private ILogger<WeatherForecastController> _logger;
        //private UserService<UserChildService> _userService;
        private UserChildService _userChildService;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, UserChildService userChildService)
        {
            _logger = logger;
            _userChildService = userChildService;
        }

        public class Test {
            public string Name { get; set; }

            public class TestInner
            {
                public string Name { get; set; }
            }
        }

        [HttpPost]
        [Consumes("application/xml")]
        public dynamic TestXml([FromBody] Test.TestInner tt)
        {
            return tt;
        }


        [HttpPost]
        [Consumes("application/xml")]
        public dynamic TestXml()
        {
            var body = Request.Body;
            var reader = new StreamReader(body);
            var str = reader.ReadToEndAsync().GetAwaiter().GetResult();
            //new XmlDocument().LoadXml
            return str;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        public string TaskTest()
        {
            Console.WriteLine("TaskTest Start!");
            Task.Run(Do);
            Console.WriteLine("TaskTest End!");
            return "success";
        }
        public static string GetConnectionString = "server=localhost;database=test;user=root;pwd=root;Allow User Variables=True;convert zero datetime=True;";
        public static IDbConnection Db
        {
            get
            {
                var conn = new MySqlConnection(GetConnectionString);
                return conn;
            }
        }

        public T Get<T>(string sql)
        {
            var conn = new MySqlConnection(GetConnectionString);
            
                conn.Open();
                return conn.Query<T>(sql, new { Id = 1 }).FirstOrDefault();
           
        }

        private void Do()
        {
            try
            {
            for (int i = 0; i < 20; i++)
            {

                Debug.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
                //Task.Delay(TimeSpan.FromSeconds(3)).Wait();
                //System.Threading.Thread.Sleep(3000);
                var result = DoSome();
                Debug.WriteLine($"Do - {i} - {result}");

                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Do - {ex.Message}");
            }
        }
        public string DoSome()
        {
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            return Get<string>("select Id from User");
        }


        public string Add(WeatherForecast weather)
        {
            return "success";
        }

        public void Dispose()
        {
        }
    }
}
